define(function (require,exports,module) {
    var angular = require('angular');

    require('business_modules/material/modules/video/videoService');
    require('business_modules/material/modules/uploaderService');

    var material = angular.module('material.videoDetailController',[
        'uploader'
    ]);

    material.controller('videoDetail',[
        '$scope',
        '$rootScope',
        'uploaderService',
        'videoService',
        '$routeParams',
        '$location',
        function ($scope,$rootScope,uploaderService,videoService,$routeParams,$location) {

            $scope.videoId = $routeParams.videoId;

            $scope.uploader = uploaderService.createFileUploader({
                api: "upload/upload-file",
                filters:[
                    {
                        name: 'videoFilter',
                        fn: function (item) {
                            return /\.(mp4|rm|rmvb|avi|mpeg|mpg)$/.test(item.name);
                        }
                    },{
                        name: 'sizeLimit',
                        fn: function (item) {
                            return item.size <= 20 * 1024 * 1024;
                        }
                    }
                ],
                success: function (res) {
                    $scope.video = res;
                    $rootScope.$emit('httpRequestEnd');
                    $rootScope.$emit('httpRequestSuccess','上传成功');
                },
                error: function (filter) {
                    if (filter === "videoFilter") {
                        alert('请上传正确的视频文件');
                    } else if (filter === "sizeLimit"){
                        alert('请选择20M以内的视频文件');
                    }
                },
                onBeforeUploadItem: function () {
                    $rootScope.$emit('httpRequestStart',"正在上传");
                },
                fail: function (res) {
                    $rootScope.$emit('httpRequestFail','上传失败');
                }
            });

            $scope.addVideo = function () {
                var video = $scope.video;
                if (!video) {
                    $rootScope.$emit('httpRequestFail','请先上传视频文件');
                    return;
                }
                var data = {
                    "name": video.filename,
                    "video_name":video.filename,
                    "video_url":video.file,
                    "title":$scope.title,
                    "summary":$scope.summary,
                    "size":video.size
                };

                videoService.createVideo({
                    search: {id: $scope.videoId},
                    data: {
                        type: 'video',
                        data: data
                    },
                    success: function (res) {
                        $location.path('/material/video');
                    },
                    fail: function (res) {
                        $rootScope.$emit('httpRequestFail',($scope.videoId ? '编辑' : '新建' )+ '视频失败');
                    }
                });
            };

            $scope.deleteVideo = function (videoId) {
                $scope.video = null;
            };

            if ($scope.videoId) {
                videoService.queryVideoInfo({
                    search:{
                        id: $scope.videoId
                    },
                    success: function (res) {
                        $scope.video = res.data;
                        $scope.title = $scope.video.title;
                        $scope.summary = $scope.video.summary;
                    },
                    fail: function (res) {
                        $rootScope.$emit('httpRequestFail','获取视频信息失败');
                    }
                });
            }
        }
    ]);
});
